package edu.hawaii.halealohacli.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Calendar;
import java.util.Locale;
import org.junit.Test;
import org.wattdepot.client.WattDepotClient;

/**
 * A JUnit test for testing BaseLine for correctness. 
 * @author Bryson Y. Hori
 */
public class TestBaseLine {

  SetBaseline testSB;
  String error;
  String url = "http://server.wattdepot.org:8190/wattdepot/";
  String value1 = "Ilima-A";
  String value2 = "2011-11-25";
  
  /**
   * Create a SetBaseline to test.
   */
  public TestBaseLine() {
    testSB = new SetBaseline();
  }
  
  /**
   * Test initializeCommand method for correct argument handling.
   */
  @Test
  public void testInitializeCommand() {
    // URL is blank to avoid accessing server unnecessarily
    String url2 = "http://";
    String[] args = new String[3];
    error = "testInitializeCommand failed: Expected true";
    assertTrue(error, testSB.initializeCommand(new WattDepotClient(url2), args));
    args = new String[2];
    error = "testInitializeCommand failed: Expected true";
    assertTrue(error, testSB.initializeCommand(new WattDepotClient(url2), args));
    // Initialization should fail without enough arguments
    args = new String[1];
    error = "testInitializeCommand failed: Expected false";
    assertFalse(error, testSB.initializeCommand(new WattDepotClient(url2), args));
    // Initialization should fail with too many arguments
    args = new String[4];
    error = "testInitializeCommand failed: Expected false";
    assertFalse(error, testSB.initializeCommand(new WattDepotClient(url2), args));
  }
  
  /**
   * Test getName method for correct output.
   */
  @Test
  public void testGetName() {
    String[] args = new String[3];
    testSB.initializeCommand(new WattDepotClient(url), args);
    error = "testGetName failed: Message does not match expected values";
    assertTrue(error, testSB.getName().equals("set-baseline"));
  }

  /**
   * Test getStartTime for successful date conversion.
   */
  @Test
  public void testGetStartTime() {
    // Future date
    error = "testGetStartTime failed: Expected null";
    assertNull(error, testSB.getStartTime("2015-11-15"));
  }
  
  /**
   * Test convertDate for successful date conversion.
   */
  @Test
  public void testConvertDate() {
    // Incorrectly formatted date
    error = "testConvertDate failed: Expected null";
    assertNull(error, testSB.convertDate("2011/11/15"));
    // Invalid date
    error = "testConvertDate failed: Expected null";
    assertNull(error, testSB.convertDate("2011-13-15"));
    // Valid date
    error = "testConvertDate failed: Expected not null";
    assertNotNull(error, testSB.convertDate("2011-11-15"));
  }
  
  /**
   * Test initializeData for successful data initialization.
   */
  @Test
  public void testInitializeData() {
    String[] args = new String[3];
    assertTrue(error, testSB.initializeCommand(new WattDepotClient(url), args));
    // Correct input
    error = "testInitializeData failed: Expected true";
    assertTrue(error, testSB.initializeData(value1, value2));
    // Unknown tower or lounge
    error = "testInitializeData failed: Expected false";
    assertFalse(error, testSB.initializeData("Cosmo-P", "2011-11-25"));
    // Bad date range
    error = "testInitializeData failed: Expected false";
    assertFalse(error, testSB.initializeData(value1, "2000-11-25"));
  }
  
  /**
   * Test toXMLGregorianCalendar for successful conversion.
   */
  @Test
  public void testToXMLGregorianCalendar() {
    Calendar date = Calendar.getInstance(Locale.US);
    error = "testToXMLGregorianCalendar failed: Expected true";
    assertNotNull(error, testSB.toXMLGregorianCalendar(date));
  }
  
  /**
   * Test GetBaselineValue for returning the correct number of values. 
   */
  public void testGetBaselineValue() {
    String[] args = new String[3];
    error = "testGetBaselineValue failed: Expected true";
    assertTrue(error, testSB.initializeCommand(new WattDepotClient(url), args));
    // Correct input
    error = "testGetBaselineValue failed: Expected true";
    assertTrue(error, testSB.initializeData(value1, value2));
    // Correct number of values
    error = "testGetBaselineValue failed: Expected value of 24.";
    assertEquals(error, 24, testSB.getBaselineValueForJohn().size());
  }
  
  /**
   * Test GetTimeValue for returning the correct number of values.
   */
  public void testGetTimeValue() {
    String[] args = new String[3];
    error = "testGetTimeValue failed: Expected true";
    assertTrue(error, testSB.initializeCommand(new WattDepotClient(url), args));
    // Correct input
    error = "testGetTimeValue failed: Expected true";
    assertTrue(error, testSB.initializeData(value1, value2));
    // Correct number of values
    error = "testGetTimeValue failed: Expected value of 24.";
    assertEquals(error, 24, testSB.getTimeValueForJohn().size());
  }
}
